﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;

public partial class Services : System.Web.UI.Page
{
    ServerObject mServerObject = null;

    protected override void InitializeCulture()
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(HttpContext.Current.Request.UserLanguages[0]);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(HttpContext.Current.Request.UserLanguages[0]);

        base.InitializeCulture();
    }

    private bool CheckServerObject()
    {
        bool result = false;

        if (Session["ServerModule"] != null)
        {
            mServerObject = (ServerObject)Session["ServerModule"];
        }

        if (mServerObject != null)
        {
            result = true;
        }

        return result;
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels

        servicesListPanel.Visible = false;
        AddNewSubcategoryServicePanel.Visible = false;
        editSubcategoryServicePanel.Visible = false;       

        #endregion Hide panels

        try
        {
            if (CheckServerObject())
            {
                #region Get Panel Name

                switch (panelName)
                {
                    #region Services
                    case "servicesListPanel":
                        servicesListPanel.Visible = true;
                        mServerObject.InfoText(this, "Lista Serviciilor");

                        if (!IsPostBack)
                        {
                            DataTable categoryList = mServerObject.GetServicesCategoryList();
                            mServerObject.fillSelector(servicesCategoryDropDownList, categoryList, "Denumirea", "ID Service");
                        }
                        break;

                    case "AddNewSubcategoryServicePanel":

                        AddNewSubcategoryServicePanel.Visible = true;
                        mServerObject.InfoText(this, "Adaugarea Noului Serviciu");
                        {
                            DataTable categoryList = mServerObject.GetServicesCategoryList();
                            mServerObject.fillSelector(addNewSubcategoryServiceDropDownList, categoryList, "Denumirea", "ID Service");
                        }

                        break;

                    case "editSubcategoryServicePanel":
                        editSubcategoryServicePanel.Visible = true;
                        mServerObject.InfoText(this, "Editarea Serviciu");
                        break;

                    #endregion Services

                    default:
                        break;
                }

                #endregion Get Panel Name
            }
        }
        catch (Exception exception)
        {
            mServerObject.LogFatal(exception.Message);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["action"] != null)
        {
            string action = Request["action"].ToString();

            switch (action)
            {
                case "logout":
                    Session["UserData"] = null;
                    Session["ServerModule"] = null;
                    Response.Redirect("Default.aspx", true);
                    break;
                default:
                    break;
            }
        }

        string areaGUID = string.Empty;
        if (Request["area"] != null)
        {
            areaGUID = Request["area"].ToString();
        }

        DataTable userData = (DataTable)Session["UserData"];

        #region Display Panels depending of area
        if (userData != null && userData.Rows.Count > 0)
        {
            if (CheckServerObject())
            {
                if (!IsPostBack)
                {
                    string areaName = areaGUID; //mServerObject.GetAreaName(areaGUID);
                    // area for each of administration entry points
                    switch (areaName)
                    {
                        case "servicesList":
                            ShowPanel(servicesListPanel.ID);
                            break;

                        default:
                            ShowPanel(string.Empty);
                            mServerObject.InfoText(this, "Please select area from left panel menu");
                            break;
                    }
                }
            }
        }

        #endregion Display Panels depending of area
    }



    #region Services Panel Events
    protected void servicesCategoryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            FillServiceListGridView(servicesCategoryDropDownList.SelectedValue);
        }

        ShowPanel(servicesListPanel.ID);
    }

    protected void servicesListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        servicesListGridView.PageIndex = e.NewPageIndex;
    }

    protected void servicesListDeleteButton_Click(object sender, GridViewDeleteEventArgs e)
    {
        if (CheckServerObject())
        {
            int rowIndex = e.RowIndex;
            string serviceIDString = servicesListGridView.Rows[rowIndex].Cells[0].Text;
            int serviceID = 0;
            int.TryParse(serviceIDString, out serviceID);
            mServerObject.DeleteService(serviceID);
            FillServiceListGridView(servicesCategoryDropDownList.SelectedValue);
        }
    }

    protected void servicesListEditButton_Click(object sender, GridViewEditEventArgs e)
    {
        if (CheckServerObject())
        {
            DataTable categoryList = mServerObject.GetServicesCategoryList();
            mServerObject.fillSelector(editSubcategoryServiceDropDownList, categoryList, "Denumirea", "ID Service");

            int rowIndex = e.NewEditIndex;

            try
            {
                string serviceIDString = servicesListGridView.Rows[rowIndex].Cells[0].Text;
                string denumirea = servicesListGridView.Rows[rowIndex].Cells[1].Text;

                editSubcategoryServiceDropDownList.SelectedValue = servicesCategoryDropDownList.SelectedValue;
                editSubcategoryServiceIDLabel.Text = serviceIDString;
                editSubcategoryServiceDenumireTextBox.Text = denumirea;
            }
            catch
            {
            }

            ShowPanel(editSubcategoryServicePanel.ID);
        }
    }

    protected void newServiceCategoryButton_Click(object sender, EventArgs e)
    {
        addNewSecriveCategoryNewDenumireTextBox.Visible = true;
        addNewSecriveCategorySaveButton.Visible = true;
        addNewSecriveCategoryCancelButton.Visible = true;
        deleteSelectedCategoryButton.Visible = false;
    }

    protected void deleteSelectedCategoryButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            int serviceCategoryID = -1;
            string stringserviceCatID = servicesCategoryDropDownList.SelectedValue;
            int.TryParse(stringserviceCatID, out serviceCategoryID);

            if (serviceCategoryID != -1)
            {
                mServerObject.DeleteServiceCategory(serviceCategoryID);
                DataTable categoryList = mServerObject.GetServicesCategoryList();
                mServerObject.fillSelector(servicesCategoryDropDownList, categoryList, "Denumirea", "ID Service");
                FillServiceListGridView(servicesCategoryDropDownList.SelectedValue);
                ShowPanel(servicesListPanel.ID);
            }
        }
    }

    protected void addNewSecriveCategorySaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            string denumireNewCat = addNewSecriveCategoryNewDenumireTextBox.Text.Trim();
            if (!denumireNewCat.Equals(string.Empty))
            {
                if (mServerObject.AddNewServiceCategory(denumireNewCat))
                {
                    mServerObject.InfoText(this, "New Service Category was saved succesiful!");
                    DataTable categoryList = mServerObject.GetServicesCategoryList();
                    mServerObject.fillSelector(servicesCategoryDropDownList, categoryList, "Denumirea", "ID Service");
                }
                else
                {
                    mServerObject.InfoText(this, "Sorry! Data Not Saved!");
                }
            }
        }

        addNewSecriveCategoryNewDenumireTextBox.Visible = false;
        addNewSecriveCategorySaveButton.Visible = false;
        addNewSecriveCategoryCancelButton.Visible = false;
        deleteSelectedCategoryButton.Visible = true;
    }

    protected void addNewSecriveCategoryCancelButton_Click(object sender, EventArgs e)
    {
        addNewSecriveCategoryNewDenumireTextBox.Visible = false;
        addNewSecriveCategorySaveButton.Visible = false;
        addNewSecriveCategoryCancelButton.Visible = false;
        deleteSelectedCategoryButton.Visible = true;
    }

    protected void FillServiceListGridView(string inputID)
    {
        int idService = 0;
        if (inputID != null)
            int.TryParse(inputID, out idService);

        if (idService != 0)
        {
            DataTable consulateList = mServerObject.GetServicesList(idService);
            servicesListGridView.DataSource = consulateList;
            servicesListGridView.DataBind();
        }
    }

    protected void addNewSubcategoryServiceSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            string denumireNewService = addNewSubcategoryServiceTextBox.Text.Trim();
            string stringIdCategory = addNewSubcategoryServiceDropDownList.SelectedValue;
            int categoryId = 0;
            int.TryParse(stringIdCategory, out categoryId);

            if (!denumireNewService.Equals(string.Empty) && categoryId != 0)
            {
                if (mServerObject.AddNewService(categoryId, denumireNewService))
                {
                    mServerObject.InfoText(this, "New Service was saved succesiful!");
                    DataTable categoryList = mServerObject.GetServicesCategoryList();
                    mServerObject.fillSelector(servicesCategoryDropDownList, categoryList, "Denumirea", "ID Service");
                    ShowPanel(servicesListPanel.ID);

                    FillServiceListGridView(servicesCategoryDropDownList.SelectedValue);
                }
                else
                {
                    mServerObject.InfoText(this, "Sorry! Data Not Saved!");
                }
            }
        }
    }

    protected void addNewSubcategoryServiceCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(servicesListPanel.ID);
    }

    protected void addNewSubserviciuButton_Click(object sender, EventArgs e)
    {
        ShowPanel(AddNewSubcategoryServicePanel.ID);
    }

    protected void editSubcategoryServiceSaveButton_Click(object sender, EventArgs e)
    {
        if (CheckServerObject())
        {
            try
            {
                string stringIdSubservice = editSubcategoryServiceIDLabel.Text;
                int idSubservice = 0;
                int.TryParse(stringIdSubservice, out idSubservice);

                string stringIDCategory = editSubcategoryServiceDropDownList.SelectedValue;
                int idCategory = 0;
                int.TryParse(stringIDCategory, out idCategory);

                string denumirea = editSubcategoryServiceDenumireTextBox.Text;

                if (idSubservice != 0 && idCategory != 0 && mServerObject.UpdateService(idSubservice, idCategory, denumirea))
                {
                    mServerObject.InfoText(this, "For " + denumirea + " all data succesiful saved!");
                }
                else
                {
                    mServerObject.InfoText(this, "For " + denumirea + " Records NOT SAVED !");
                }
            }
            catch (Exception ex)
            {
                mServerObject.InfoText(this, "Unable to save Service" + ex.Message);
            }
        }

        ShowPanel(servicesListPanel.ID);
    }

    protected void editSubcategoryServiceCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(servicesListPanel.ID);
    }

    #endregion Services Panel Events
}